package com.seeyoui.kensite.framework.file.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;

import com.seeyoui.kensite.framework.file.model.FileAttribute;
import com.seeyoui.kensite.framework.file.model.ReturnResponse;
import com.seeyoui.kensite.framework.file.service.FilePreview;
import com.seeyoui.kensite.framework.file.utils.ChangeFileEncoding;
import com.seeyoui.kensite.framework.file.utils.DownloadUtils;
import com.seeyoui.kensite.framework.file.utils.FileUtils;
import com.seeyoui.kensite.framework.file.utils.OfficeToPdf;
import com.sun.star.uno.Exception;

import java.io.File;
import java.io.IOException;
import java.util.PropertyResourceBundle;

/**
 * Created by kl on 2018/1/17. Content :处理office文件
 */
@Service
public class OfficeFilePreviewImpl implements FilePreview {

    PropertyResourceBundle prb = (PropertyResourceBundle) PropertyResourceBundle.getBundle("file-management");
    String fileDir = prb.getString("file.upload.url");
    @Override
    public String[] filePreviewHandle(String url, Model model) {
        FileAttribute fileAttribute = null;
        try {
            fileAttribute = FileUtils.getFileAttribute(url);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String suffix = fileAttribute.getSuffix();
        String fileName = fileAttribute.getName();
        String decodedUrl = fileAttribute.getDecodedUrl();
        boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx");
        String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf");
        String filePath = fileDir + fileName;
        if (!new File(filePath).exists()) {
            ReturnResponse<String> response = DownloadUtils.downLoad(decodedUrl, suffix, null);
            if (0 != response.getCode()) {
                model.addAttribute("msg", response.getMsg());
                return new String[]{"fileNotSupported","fileNotSupported"};
            }
            filePath = response.getContent();
        }
        String outFilePath = fileDir + pdfName;
        if (StringUtils.hasText(outFilePath)) {
            OfficeToPdf.openOfficeToPDF(filePath, outFilePath);
            String aaa = FileUtils.getFileEncodeUTFGBK(outFilePath);
            File f = new File(filePath);
            if (f.exists()) {
                f.delete();
            }
            if (isHtml) {
                // 对转换后的文件进行操作(改变编码方式)
                FileUtils.doActionConvertedFile(outFilePath);
                // 将文件编码格式转换为utf-8
                try {
                    ChangeFileEncoding.convert(outFilePath);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }            
        }
        model.addAttribute("pdfUrl", pdfName);
        return new String[]{isHtml ? "html" : "pdf",fileName};
    }
}
